package com.zlj.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1、得到用户名；
		 * 2、然后进行判断：
		 *     如果包含admin,则就是管理员
		 *     如果没有包含，则是普通用户
		 * 3、 把登录的用户名保存到session中
		 * 4、 转发到index.jsp中
		 */
	    String username = request.getParameter("username");
	    System.out.println(username+"--------------");
	    if(username == null || username.isEmpty()) {
	    	request.getRequestDispatcher("/index.jsp").forward(request, response);
	    } else {
		    if(username.contains("admin")) {
		    	request.getSession().setAttribute("admin", username);
		    	request.getRequestDispatcher("/admin/admin.jsp").forward(request, response);
		    	return;
		    } else {
		    	request.getSession().setAttribute("user", username);
		    	request.getRequestDispatcher("/user/user.jsp").forward(request, response);
		    	return;
		    } 
	    }
	}
}
